package com.imooc.controller;

import com.imooc.pojo.Users;
import com.imooc.pojo.bo.UserBO;
import com.imooc.service.UserService;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: ldk
 * 描述: TODO
 */
@RestController
@Api(value = "注册登录", tags = "注册登录")
@RequestMapping("/passport")
public class PassportController {
    @Autowired
    private UserService userService;
    @ApiOperation(value = "用户是否存在", notes = "判断用户是否存在",httpMethod = "GET")
    @GetMapping("/userNameIsExist")
    //public int userNameIsExist(@RequestParam String userName) {
    //public HttpStatus userNameIsExist(@RequestParam String userName) {
    public IMOOCJSONResult userNameIsExist(@RequestParam String userName) {


        //StringUtils是Apache下的commons-lang3的工具,使用其isBank方法
        //1、判断用户名不能为空
        if (StringUtils.isBlank(userName)) {
            //return 500;
            //return HttpStatus.INTERNAL_SERVER_ERROR;
            return IMOOCJSONResult.errorMsg("请输入用户名");
        }
        //2、查找注册的用户是否存在
        boolean isExist = userService.queryUsernameIsExist(userName);
        if (isExist) {
            //return 500;
            //return HttpStatus.INTERNAL_SERVER_ERROR;
            return IMOOCJSONResult.errorMsg("用户名已存在");
        }
        //3、请求成功，用户名没有重复
        //return 200
        //return HttpStatus.OK;
        return IMOOCJSONResult.ok();
    }


    /**
     * 注册用户
     * @param userBO 注册信息
     * @return 注册结果
     */
    @ApiOperation(value = "用户注册",notes = "用户注册",httpMethod = "POST")
    @PostMapping("/regist")
    public IMOOCJSONResult regist(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response) {
        //获取注册输入信息
        String userName = userBO.getUsername();
        String password = userBO.getPassword();
        String confirmPassword = userBO.getConfirmPassword();

        //1、判断输入注册内容是否为空
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(confirmPassword)){
            return IMOOCJSONResult.errorMsg("用户名或密码不能为空");
        }
        //2、输入密码长度不能小于6位
        if (password.length() < 6){
            return IMOOCJSONResult.errorMsg("密码长度不能小于6位");
        }
        //3、判断两次输入密码是否一致
        if (!password.equals(confirmPassword)){
            return IMOOCJSONResult.errorMsg("两次输入的密码不一致");
        }
        //4、查询用户名是否已经存在
        boolean isExist = userService.queryUsernameIsExist(userName);
        if (isExist){
            return IMOOCJSONResult.errorMsg("用户名已存在");
        }
        //5、注册成功
        Users userResult = userService.createUser(userBO);
        userResult = setNullProperty(userResult);
        CookieUtils.setCookie(request,response,"user",JsonUtils.objectToJson(userResult),true);


        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "用户登录",notes = "用户登录",httpMethod = "POST")
    @PostMapping("/login")
    public IMOOCJSONResult login(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取注册输入信息
        String userName = userBO.getUsername();
        String password = userBO.getPassword();

        //1、判断输入注册内容是否为空
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password)){
            return IMOOCJSONResult.errorMsg("用户名或密码不能为空");
        }
        //2、实现用户登录
        Users userResult = userService.queryUserForLogin(userName, MD5Utils.getMD5Str(password));
        if (userResult == null) {
            return IMOOCJSONResult.errorMsg("用户名或密码错误");
        }
        userResult = setNullProperty(userResult);

        CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(userResult), true);

        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "用户退出登录",notes = "用户退出登录",httpMethod = "POST")
    @PostMapping("/logout")
    public IMOOCJSONResult logout(@RequestParam String userId, HttpServletRequest request, HttpServletResponse response) throws Exception {
       //清楚用户相关的cookie信息
        CookieUtils.deleteCookie(request,response,"user");

        //TODO 用户退出登录，清空购物车

        // TODO 分布式会话中，需要清除用户数据

        return IMOOCJSONResult.ok();
    }

    /**
     * 将一些属性忽略掉
     * @param userResult 操作对象
     * @return
     */
    public Users setNullProperty(Users userResult) {
        userResult.setBirthday(null);
        userResult.setPassword(null);
        //userResult.setRealname(null);
        userResult.setCreatedTime(null);
        userResult.setUpdatedTime(null);
        userResult.setEmail(null);
        userResult.setMobile(null);
        //userResult.setSex(null);
        return userResult;
    }


}
